package com.zenithmind.library.controller;

import com.zenithmind.common.result.Result;
import com.zenithmind.library.service.BookStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 图书统计管理控制器
 * 遵循单一职责原则：只负责图书统计相关的HTTP请求处理
 * 遵循依赖倒置原则：依赖抽象接口而非具体实现
 * 
 * @author ZenithMind
 * @since 2024-06-14
 */
@Slf4j
@RestController
@RequestMapping("/api/library/book-statistics")
@RequiredArgsConstructor
@Tag(name = "图书统计管理", description = "图书统计数据的查询、更新、分析等功能")
public class BookStatisticsController {

    private final BookStatisticsService bookStatisticsService;

    @PostMapping("/{bookId}/review/add")
    @Operation(summary = "添加图书评价统计", description = "为图书添加评价统计信息")
    @PreAuthorize("hasAuthority('library:statistics:update')")
    public Result<Void> addBookReview(
            @Parameter(description = "图书ID", required = true)
            @PathVariable String bookId,
            @Parameter(description = "评分", required = true)
            @RequestParam Integer rating,
            @Parameter(description = "是否好评", required = true)
            @RequestParam boolean isPositive) {
        log.info("添加图书评价统计，图书ID：{}，评分：{}，是否好评：{}", bookId, rating, isPositive);
        return bookStatisticsService.addBookReview(bookId, rating, isPositive);
    }

    @DeleteMapping("/{bookId}/review/remove")
    @Operation(summary = "移除图书评价统计", description = "移除图书评价统计信息")
    @PreAuthorize("hasAuthority('library:statistics:update')")
    public Result<Void> removeBookReview(
            @Parameter(description = "图书ID", required = true)
            @PathVariable String bookId,
            @Parameter(description = "是否为好评", required = true)
            @RequestParam boolean wasPositive) {
        log.info("移除图书评价统计，图书ID：{}，是否为好评：{}", bookId, wasPositive);
        return bookStatisticsService.removeBookReview(bookId, wasPositive);
    }

    @PostMapping("/{bookId}/view/increment")
    @Operation(summary = "增加浏览次数", description = "增加图书的浏览次数统计")
    @PreAuthorize("hasAuthority('library:statistics:update')")
    public Result<Void> incrementViewCount(
            @Parameter(description = "图书ID", required = true)
            @PathVariable String bookId) {
        log.debug("增加图书浏览次数，图书ID：{}", bookId);
        return bookStatisticsService.incrementViewCount(bookId);
    }

    @PostMapping("/{bookId}/favorite/increment")
    @Operation(summary = "增加收藏次数", description = "增加图书的收藏次数统计")
    @PreAuthorize("hasAuthority('library:statistics:update')")
    public Result<Void> incrementFavoriteCount(
            @Parameter(description = "图书ID", required = true)
            @PathVariable String bookId) {
        log.info("增加图书收藏次数，图书ID：{}", bookId);
        return bookStatisticsService.incrementFavoriteCount(bookId);
    }

    @PostMapping("/{bookId}/favorite/decrement")
    @Operation(summary = "减少收藏次数", description = "减少图书的收藏次数统计")
    @PreAuthorize("hasAuthority('library:statistics:update')")
    public Result<Void> decrementFavoriteCount(
            @Parameter(description = "图书ID", required = true)
            @PathVariable String bookId) {
        log.info("减少图书收藏次数，图书ID：{}", bookId);
        return bookStatisticsService.decrementFavoriteCount(bookId);
    }

    @PostMapping("/{bookId}/share/increment")
    @Operation(summary = "增加分享次数", description = "增加图书的分享次数统计")
    @PreAuthorize("hasAuthority('library:statistics:update')")
    public Result<Void> incrementShareCount(
            @Parameter(description = "图书ID", required = true)
            @PathVariable String bookId) {
        log.info("增加图书分享次数，图书ID：{}", bookId);
        return bookStatisticsService.incrementShareCount(bookId);
    }

    @PostMapping("/{bookId}/borrow/increment")
    @Operation(summary = "增加借阅次数", description = "增加图书的借阅次数统计")
    @PreAuthorize("hasAuthority('library:statistics:update')")
    public Result<Void> incrementBorrowCount(
            @Parameter(description = "图书ID", required = true)
            @PathVariable String bookId) {
        log.info("增加图书借阅次数，图书ID：{}", bookId);
        return bookStatisticsService.incrementBorrowCount(bookId);
    }

    @PutMapping("/{bookId}/rating")
    @Operation(summary = "更新图书评分", description = "更新图书的平均评分")
    @PreAuthorize("hasAuthority('library:statistics:update')")
    public Result<Void> updateBookRating(
            @Parameter(description = "图书ID", required = true)
            @PathVariable String bookId,
            @Parameter(description = "评分", required = true)
            @RequestParam Double rating) {
        log.info("更新图书评分，图书ID：{}，评分：{}", bookId, rating);
        return bookStatisticsService.updateBookRating(bookId, rating);
    }
}
